package com.danskin.wechat.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.danskin.wechat.api.client.WeiXinOpenApiClient;
import com.danskin.wechat.dto.AccumulativeTotalFansDto;
import com.danskin.wechat.dto.IncreasedAndLostFansDto;

@Controller
@RequestMapping("userAnalyze")
public class UserAnalyzeController extends BaseController {

	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 跳转到用户分析报表页面
	 * 
	 * @param model
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "jumpUserAnalyze", method = RequestMethod.GET)
	public String jumpUserAnalyze(Model model) {

		String beginDateSevenDays = sdf.format(com.danskin.wechat.util.DateUtils.getBeforeDay(-7));

		String endDate = sdf.format(com.danskin.wechat.util.DateUtils.getBeforeDay(-1));

		List<IncreasedAndLostFansDto> list = new ArrayList<IncreasedAndLostFansDto>();

		// 昨天新增和取消关注用户
		List<IncreasedAndLostFansDto> increasedAndLostFansListYesterday = new ArrayList<IncreasedAndLostFansDto>();
		List<IncreasedAndLostFansDto> increasedAndLostFansListYesterdayTemp = new ArrayList<IncreasedAndLostFansDto>();
		// 最近七天新增和取消关注用户
		List<IncreasedAndLostFansDto> increasedAndLostFansListSevenDays = new ArrayList<IncreasedAndLostFansDto>();
		List<IncreasedAndLostFansDto> increasedAndLostFansListSevenDaysTemp = new ArrayList<IncreasedAndLostFansDto>();
		// 昨天累计用户
		List<AccumulativeTotalFansDto> accumulativeTotalFansListYesterday = new ArrayList<AccumulativeTotalFansDto>();
		// 最近七天累计用户
		List<AccumulativeTotalFansDto> accumulativeTotalFansListSevenDays = new ArrayList<AccumulativeTotalFansDto>();
		Map<String, IncreasedAndLostFansDto> map = new HashMap<String, IncreasedAndLostFansDto>();// 合并后的IncreasedAndLostFansDto存在map中

		try {
			// 获取mpId
			Integer mpId = getMpId();
			// 获取微信公众号access_token
			String accessToken = getMPWithAccessToken(mpId);

			// 调用微信接口获取昨天的新增和取消关注的用户数
			String increasedAndLostFansYesterdayString = WeiXinOpenApiClient.createInstance()
					.getIncreasedAndLostWxFansCount(accessToken, endDate, endDate);
			com.alibaba.fastjson.JSONObject increasedAndLostFansYesterdayJSON = JSON
					.parseObject(increasedAndLostFansYesterdayString);
			if (increasedAndLostFansYesterdayJSON.containsKey("list")) {
				increasedAndLostFansListYesterdayTemp = JSON
						.parseArray(increasedAndLostFansYesterdayJSON.getString("list"), IncreasedAndLostFansDto.class);
			}

			// 调用微信接口获取7天的新增和取消关注的用户数
			String increasedAndLostFansSevenDaysString = WeiXinOpenApiClient.createInstance()
					.getIncreasedAndLostWxFansCount(accessToken, beginDateSevenDays, endDate);
			com.alibaba.fastjson.JSONObject increasedAndLostFansSevenDaysJSON = JSON
					.parseObject(increasedAndLostFansSevenDaysString);
			if (increasedAndLostFansSevenDaysJSON.containsKey("list")) {
				increasedAndLostFansListSevenDaysTemp = JSON
						.parseArray(increasedAndLostFansSevenDaysJSON.getString("list"), IncreasedAndLostFansDto.class);
			}

			// 调用微信接口获取昨天累计用户数
			String accumulativeTotalFansYesterdayString = WeiXinOpenApiClient.createInstance()
					.getAccumulativeTotalWxFansCount(accessToken, endDate, endDate);
			com.alibaba.fastjson.JSONObject accumulativeTotalFansYesterdayJSON = JSON
					.parseObject(accumulativeTotalFansYesterdayString);
			if (accumulativeTotalFansYesterdayJSON.containsKey("list")) {
				accumulativeTotalFansListYesterday = JSON.parseArray(
						accumulativeTotalFansYesterdayJSON.getString("list"), AccumulativeTotalFansDto.class);
			}

			// 调用微信接口获取7天累计用户数
			String accumulativeTotalFansSevenDaysString = WeiXinOpenApiClient.createInstance()
					.getAccumulativeTotalWxFansCount(accessToken, beginDateSevenDays, endDate);
			com.alibaba.fastjson.JSONObject accumulativeTotalFansSevenDaysJSON = JSON
					.parseObject(accumulativeTotalFansSevenDaysString);
			if (accumulativeTotalFansSevenDaysJSON.containsKey("list")) {
				accumulativeTotalFansListSevenDays = JSON.parseArray(
						accumulativeTotalFansSevenDaysJSON.getString("list"), AccumulativeTotalFansDto.class);
			}
			
			// 处理数据(把相同时间的数据合并为一条数据)
			for (IncreasedAndLostFansDto temp : increasedAndLostFansListYesterdayTemp) {
				boolean state = false;
				for (IncreasedAndLostFansDto temp1 : increasedAndLostFansListYesterday) {
					if (temp1.getRef_date().equals(temp.getRef_date())) {
						int newUserCount = temp1.getNew_user();
						newUserCount += temp.getNew_user();
						temp1.setNew_user(newUserCount);
						int cancalUserCount = temp1.getCancel_user();
						cancalUserCount += temp.getCancel_user();
						temp1.setCancel_user(cancalUserCount);
						state = true;
					}
				}
				if (!state) {
					increasedAndLostFansListYesterday.add(temp);
				}
			}

			// 处理数据(把相同时间的数据合并为一条数据)
			for (IncreasedAndLostFansDto temp : increasedAndLostFansListSevenDaysTemp) {
				boolean state = false;
				for (IncreasedAndLostFansDto temp1 : increasedAndLostFansListSevenDays) {
					if (temp1.getRef_date().equals(temp.getRef_date())) {
						int newUserCount = temp1.getNew_user();
						newUserCount += temp.getNew_user();
						temp1.setNew_user(newUserCount);
						int cancalUserCount = temp1.getCancel_user();
						cancalUserCount += temp.getCancel_user();
						temp1.setCancel_user(cancalUserCount);
						state = true;
					}
				}
				if (!state) {
					increasedAndLostFansListSevenDays.add(temp);
				}
			}

			// increasedAndLostFansListSevenDays.addAll(accumulativeTotalFansListSevenDays);
			list.addAll(increasedAndLostFansListSevenDays);
			list.addAll(accumulativeTotalFansListSevenDays);

			IncreasedAndLostFansDto param = null;
			for (IncreasedAndLostFansDto temp : list) {
				param = map.get(temp.getRef_date());
				if (param != null) {
					param.setCumulate_user(temp.getCumulate_user());
				} else {
					map.put(temp.getRef_date(), temp);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		model.addAttribute("increasedAndLostFansListYesterday", increasedAndLostFansListYesterday);
		model.addAttribute("increasedAndLostFansListSevenDays", increasedAndLostFansListSevenDays);
		model.addAttribute("accumulativeTotalFansListYesterday", accumulativeTotalFansListYesterday);
		model.addAttribute("accumulativeTotalFansListSevenDays", accumulativeTotalFansListSevenDays);
		model.addAttribute("map", map);

		return "reportStatistics/user_analyze";
	}

}
